package com.jit.monitor.util;

import android.text.TextUtils;
import android.util.Log;

import com.jit.monitor.app.MyApp;
import com.jit.monitor.model.cache.UserCache;
import cn.jpush.android.api.JPushInterface;
/**
 * @author crazyZhangxl on 2019/1/15.
 * Describe:
 */
public class JPushHelper {
    private String TAG = "JPushHelper";
    public long currentTime = 0L;
    private JPushHelper(){

    }

    public long getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(long currentTime) {
        this.currentTime = currentTime;
    }

    private static  class JPushHelperHolder{
        private static final JPushHelper MINSTANCE = new JPushHelper();
    }

    public static JPushHelper getInstance(){
        return JPushHelperHolder.MINSTANCE;
    }



    /**
     * 设置别名与标签
     *
     * @param alias
     */
    private void setAlias(String alias) {
        if (null != alias) {
            //恢复接收推送
            JPushInterface.resumePush(MyApp.getContext());
            JPushInterface.setAlias(MyApp.getContext(), 123,alias);
        }
    }

    public void setAlias(){
        if (UserCache.getUserID() != -1){
            Log.e(TAG, "setAlias: "+ UserCache.getUserID());
            setAlias(String.valueOf(UserCache.getUserID()));
        }
    }

    /**
     * 停止接收推送
     */
    public void removeAlias() {
        JPushInterface.clearAllNotifications(MyApp.getContext());
        //该句打开的话，如果退出登录后，用户就收不到离线的消息了。
        setAlias("");
        JPushInterface.stopPush(MyApp.getContext());
    }

}
